<?php

/**
 * Контроллер меню
 * @author andrew
 *
 */
class Navigation_MenuController extends Zend_Controller_Action {
	
	/**
	 * Список всех меню
	 */
	public function indexAction() {
		$this->view->headTitle($this->view->translate('Menu Listing'));	
		
		$menu = Doctrine_Core::getTable('Navigation_Model_Menu')->findAll();
		
		$this->view->menu = $menu;
	}
	
	/**
	 * Создание меню
	 */
	public function createAction() {
		$form = new Navigation_Form_Menu();
		$form->setAction($this->view->url(array(),'navigation_menu_create'));
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$menu = new Navigation_Model_Menu();
			$menu->name = $values['name'];
			$menu->save();
			
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Menu has been save successfully!'));
			return $this->_redirect($this->view->url(array(),'navigation_menu_index'));
		}
		
		$this->view->form = $form;
	}
	
	/**
	 * Обновление меню
	 * @throws Zend_Controller_Action_Exception
	 */
	public function updateAction() {
		$id = $this->_request->getParam('id',0);
		
		$menu = Doctrine_Core::getTable('Navigation_Model_Menu')->find($id);
		
		if(!$id || !$menu)
			throw new Zend_Controller_Action_Exception('Menu not found!', 404);
		
		$populate = array(
			'name' => $menu->name
		);
		
		$form = new Navigation_Form_Menu();
		$form->setAction($this->view->url(array('id' => $id),'navigation_menu_update'));
		$this->view->form = $form;
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$menu->name = $values['name'];
			$menu->save();
			
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Menu has been save successfully!'));
			return $this->_redirect($this->view->url(array(),'navigation_menu_index'));
		} else {
			$form->populate($populate);
		}		
	}
	
	/**
	 * Удаление меню
	 * @throws Zend_Controller_Action_Exception
	 */
	public function deleteAction() {
		$id = $this->_request->getParam('id',0);
		
		$menu = Doctrine_Core::getTable('Navigation_Model_Menu')->find($id);
		
		if(!$id || !$menu)
			throw new Zend_Controller_Action_Exception('Menu not found!', 404);
			
		$menu->delete();
		$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Menu has been delete successfully!'));
		return $this->_redirect($this->view->url(array(),'navigation_menu_index'));
	}
}